package com.pktechan.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.pktechan.api.dto.MiniSubscribeMessage;
import com.pktechan.api.entity.WxMp;
import com.pktechan.api.exception.CustomException;
import com.pktechan.api.service.SendMessageService;
import com.pktechan.api.service.WxMpService;
import com.pktechan.api.utils.HtmlUtil;
import com.pktechan.api.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: cloudfloating
 * @description: 发送消息
 * @author: qx.zhang
 * @create: 2021-03-23 11:27
 **/
@RestController
@RequestMapping("sendMessageController")
public class SendMessageController {

    private static final Logger logger = LoggerFactory.getLogger(SendMessageController.class);

    @Autowired
    private SendMessageService sendMessageService;

    @Autowired
    private WxMpService wxMpService;

    /**
     * 小程序订阅消息发送
     */
    @RequestMapping(value = "/sendSubscribeMessage", method = {RequestMethod.GET, RequestMethod.POST})
    public void sendSubscribeMessage(@RequestParam(value = "subscribeMessageJson", required = true) String subscribeMessageJson, HttpServletRequest request, HttpServletResponse response) {
        logger.info("-----------小程序订阅消息发送 sendTemplateMessage的请求-----------");
        try {
            synchronized (this) {
                logger.info("-----------发送小程序订阅消息开始-----------");
                Long startTime = System.currentTimeMillis();
                MiniSubscribeMessage miniSubscribeMessage = null;
                logger.info("开始时间：" + startTime);
                miniSubscribeMessage = JSONObject.parseObject(subscribeMessageJson, MiniSubscribeMessage.class);
                //获取发送消息的mpId
                String mpId = miniSubscribeMessage.getMpId();
                logger.info("微信小程序订阅消息中的mpId：" + mpId);
                if (StringUtil.isEmpty(mpId)) {
                    throw new CustomException("mpId为空");
                }
                logger.info("发小程序订阅消息的mpId：" + mpId);
                WxMp mp = wxMpService.getByMpId(mpId);
                String accessToken = wxMpService.getMpAccessToken(mp);
                sendMessageService.sendMiniSubscribeMessage(accessToken, miniSubscribeMessage);
                Long endTime = System.currentTimeMillis();
                logger.info("结束时间：" + endTime);
                logger.info("耗时：" + (endTime - startTime));
                logger.info("-----------发送小程序订阅消息结束-----------");
                HtmlUtil.writerSuccessJson(response,"发送成功");
            }
        } catch (CustomException e) {
            e.printStackTrace();
            HtmlUtil.writerFailJson(response, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            HtmlUtil.writerFailJson(response, "消息发送失败");
        }
    }

}
